package com.zuberot.apnmacedonia;

import com.zuberot.apnmacedonia.operators.OneMK;
import com.zuberot.apnmacedonia.operators.TMobileMK;
import com.zuberot.apnmacedonia.operators.VipMK;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Viktor Stojanovski - (zuberot@gmail.com)
 * 
 */
public class APNMacedoniaActivity extends Activity implements OnClickListener {

	public static String logTag = "APNMacedonia";

	private TextView operatorText;
	private ImageButton operatorTMobileMK;
	private ImageButton operatorVipMK;
	private ImageButton operatorOneMK;
	private ImageView shareButton;
	private LinearLayout sadLayout;
	private ImageView sadImage;

	private boolean isTmobileMK;
	private boolean isVipMK;
	private boolean isOneMK;
	private boolean isOneMKPostpaid;

	private String shareString;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getMobileOperator();
		initView();
	}

	private void initView() {
		setContentView(R.layout.apn_macedonia);

		operatorText = (TextView) findViewById(R.id.operator_text);
		operatorTMobileMK = (ImageButton) findViewById(R.id.operator_tmobile);
		operatorVipMK = (ImageButton) findViewById(R.id.operator_vip);
		operatorOneMK = (ImageButton) findViewById(R.id.operator_one);
		shareButton = (ImageView) findViewById(R.id.share_button);
		sadLayout = (LinearLayout) findViewById(R.id.sad_layout);
		sadImage = (ImageView) findViewById(R.id.sad_image);

		operatorTMobileMK.setOnClickListener(this);
		operatorVipMK.setOnClickListener(this);
		operatorOneMK.setOnClickListener(this);
		shareButton.setOnClickListener(this);
		sadImage.setOnClickListener(this);

		if (isTmobileMK) {
			operatorText.setVisibility(View.VISIBLE);
			operatorTMobileMK.setVisibility(View.VISIBLE);
			sadLayout.setVisibility(View.GONE);
			shareButton.setVisibility(View.VISIBLE);
			shareString = new String(getResources().getString(R.string.share_poraka_tmobilemk));
		} else if (isVipMK) {
			operatorText.setVisibility(View.VISIBLE);
			operatorVipMK.setVisibility(View.VISIBLE);
			sadLayout.setVisibility(View.GONE);
			shareButton.setVisibility(View.VISIBLE);
			shareString = new String(getResources().getString(R.string.share_poraka_vipmk));
		} else if (isOneMK) {
			operatorText.setVisibility(View.VISIBLE);
			operatorOneMK.setVisibility(View.VISIBLE);
			sadLayout.setVisibility(View.GONE);
			shareButton.setVisibility(View.VISIBLE);
			shareString = new String(getResources().getString(R.string.share_poraka_onemk));
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.operator_tmobile:
			doTMobileMK();
			break;
		case R.id.operator_vip:
			doVipMK();
			break;
		case R.id.operator_one:
			doOneMK();
			break;
		case R.id.share_button:
			shareIntent();
			break;
		case R.id.sad_image:
			finish();
			break;
		default:
			break;
		}
	}

	private void doTMobileMK() {
		TMobileMK tMobileMK = new TMobileMK();
		int tMobileId = APNFuntions.insertAPNInternet(getApplicationContext(), tMobileMK);
		APNFuntions.setDefaultAPN(getApplicationContext(), tMobileId);
		APNFuntions.insertAPNMMS(getApplicationContext(), tMobileMK);
		openAPNSettings();
	}

	private void doVipMK() {
		VipMK vipMK = new VipMK();
		int vipMKId = APNFuntions.insertAPNInternet(getApplicationContext(), vipMK);
		APNFuntions.setDefaultAPN(getApplicationContext(), vipMKId);
		APNFuntions.insertAPNMMS(getApplicationContext(), vipMK);
		openAPNSettings();
	}

	private void doOneMK() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Припејд или Постпејд?")
				.setPositiveButton("Припејд", dialogPrepaidPostpaidClickListener)
				.setNegativeButton("Постпејд",dialogPrepaidPostpaidClickListener)
				.show();
	}

	DialogInterface.OnClickListener dialogPrepaidPostpaidClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case DialogInterface.BUTTON_POSITIVE: // prepaid
				isOneMKPostpaid = false;
				break;
			case DialogInterface.BUTTON_NEGATIVE: // postpaid
				isOneMKPostpaid = true;
				break;
			}
			OneMK oneMK = new OneMK(isOneMKPostpaid);
			int oneMKId = APNFuntions.insertAPNInternet(getApplicationContext(), oneMK);
			APNFuntions.setDefaultAPN(getApplicationContext(), oneMKId);
			APNFuntions.insertAPNMMS(getApplicationContext(), oneMK);
			openAPNSettings();
		}
	};

	private void getMobileOperator() {
		TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String networkOperator = tel.getNetworkOperator();

		if (networkOperator != null) {
			Integer mcc = Integer.parseInt(networkOperator.substring(0, 3));
			Integer mnc = Integer.parseInt(networkOperator.substring(3));
			String numeric = new String(mcc.toString() + "0" + mnc.toString());
			Log.i(logTag, "Mobile Operator numeric: " + numeric);
			if (numeric.equals(TMobileMK.NUMERIC)) {
				isTmobileMK = true;
			} else if (numeric.equals(VipMK.NUMERIC)) {
				isVipMK = true;
			} else if (numeric.equals(OneMK.NUMERIC)) {
				isOneMK = true;
			}
		}
	}

	private void openAPNSettings() {
		Intent apnSettings = new Intent(Settings.ACTION_APN_SETTINGS);
		apnSettings.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
		startActivity(apnSettings);
	}

	private void shareIntent() {
		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
		startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.share_title)));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			this.finish();
			break;
		case R.id.about:
			AlertDialog aboutDialog = new AlertDialog.Builder(this).create();
			aboutDialog
					.setMessage("APN Macedonia е апликација која ќе ви помогне лесно да инсталирате конфигурација за интернет и MMS на вашиот Android телефон.Работи на сите мобилни оператори VipMK, ONEmk или T-Mobile MK. \n \n"
							+ "Viktor Zuberot Stojanovski <zuberot@gmail.com> 2011 \n \n"
							+ "http://zuberot.info\n"
							+ "http://mobile.komunikacii.net\n");
			aboutDialog.setTitle("За апликацијата");
			aboutDialog.setButton("Во ред",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int arg1) {
							dialog.dismiss();
						}
					});
			aboutDialog.show();
			break;
		}
		return true;
	}

}